package com.april.product.controller;

import com.april.common.constant.MallUrl;
import com.april.common.utils.PageUtils;
import com.april.common.utils.Result;
import com.april.common.validator.group.AddGroup;
import com.april.common.validator.group.UpdateGroup;
import com.april.common.validator.group.UpdateStatusGroup;
import com.april.product.entity.BrandEntity;
import com.april.product.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 品牌
 *
 * @author april
 * @email 2673888518@qq.com
 * @date 2023-07-13 00:31:15
 */
@RestController
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping(MallUrl.product.PRODUCT_BRAND_LIST)
    public Result list(@RequestParam Map<String, Object> params) {
        PageUtils page = brandService.queryPage(params);

        return Result.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping(MallUrl.product.PRODUCT_BRAND_INFO)
    public Result info(@PathVariable("brandId") Long brandId) {
        BrandEntity brand = brandService.getById(brandId);

        return Result.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping(MallUrl.product.PRODUCT_BRAND_ADD)
    public Result save(@Validated(AddGroup.class) @RequestBody BrandEntity brand) {
        brandService.save(brand);
        return Result.ok();
    }

    /**
     * 修改
     */
    @RequestMapping(MallUrl.product.PRODUCT_BRAND_UPDATE)
    public Result update(@Validated(UpdateGroup.class) @RequestBody BrandEntity brand) {
        brandService.updateById(brand);

        return Result.ok();
    }

    /**
     * 修改状态
     */
    @RequestMapping(MallUrl.product.PRODUCT_BRAND_STATUS_UPDATE)
    public Result updateStatus(@Validated(UpdateStatusGroup.class) @RequestBody BrandEntity brand) {
        brandService.updateById(brand);

        return Result.ok();
    }

    /**
     * 删除
     */
    @RequestMapping(MallUrl.product.PRODUCT_BRAND_DELETE)
    public Result delete(@RequestBody Long[] brandIds) {
        brandService.removeByIds(Arrays.asList(brandIds));

        return Result.ok();
    }

}
